using ASP;
using HoWave.Common;
using HoWave.Web.Admin;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class HL_Admin_Admin_CheckFile : Admin, IRequiresSessionState
{
	public class MD5File : IComparable<HL_Admin_Admin_CheckFile.MD5File>
	{
		private string _md5 = "";
		private string _filepath = "";
		private int _check = 0;
		private string _length = "";
		private string _lastwritetime = "";
		private string _rootdir = "/";
		public string MD5Code
		{
			get
			{
				return this._md5;
			}
			set
			{
				this._md5 = value;
			}
		}
		public string FilePath
		{
			get
			{
				return this._filepath;
			}
			set
			{
				this._filepath = value;
			}
		}
		public int Check
		{
			get
			{
				return this._check;
			}
			set
			{
				this._check = value;
			}
		}
		public string Length
		{
			get
			{
				return this._length;
			}
			set
			{
				this._length = value;
			}
		}
		public string LastWriteTime
		{
			get
			{
				return this._lastwritetime;
			}
			set
			{
				this._lastwritetime = value;
			}
		}
		public string RootDir
		{
			get
			{
				return this._rootdir;
			}
		}
		public MD5File(string md5, string filepath, int check)
		{
			this._md5 = md5;
			this._filepath = filepath;
			this._check = check;
			int num = filepath.IndexOf("/", 1);
			if (num > 0)
			{
				this._rootdir = filepath.Substring(0, filepath.IndexOf("/", 1));
			}
		}
		public int CompareTo(HL_Admin_Admin_CheckFile.MD5File other)
		{
			return this.RootDir.CompareTo(other.RootDir);
		}
	}
	public int filenums = 0;
	private StringBuilder sb = new StringBuilder();
	private StringBuilder sb_w = new StringBuilder();
	public List<HL_Admin_Admin_CheckFile.MD5File> standardArray = new List<HL_Admin_Admin_CheckFile.MD5File>();
	public List<HL_Admin_Admin_CheckFile.MD5File> compareArray = new List<HL_Admin_Admin_CheckFile.MD5File>();
	protected Literal execute_time;
	protected Literal checkResult;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (Fetch.Get("cmd") != "")
		{
			this.CreateFile();
		}
		Stopwatch stopwatch = new Stopwatch();
		stopwatch.Reset();
		stopwatch.Start();
		this.InitStandardArray();
		this.InitCompareArray();
		base.Response.Flush();
		this.CheckResult();
		this.ShowResult();
		stopwatch.Stop();
		this.execute_time.Text = "查询耗时共:" + stopwatch.ElapsedMilliseconds + " 毫秒";
	}
	protected void ShowResult()
	{
		string format = "<table cellpadding=\"0\" cellspacing=\"0\">\r\n<thread><tr><td colspan=\"2\"><b>{0}</b></td><td colspan=\"2\">{1}</td></tr></thread><tr><th width=\"50%\">文件名</th><th  width=\"15%\">文件大小</th><th  width=\"20%\">最后修改时间</th><th  width=\"15%\">状态</th></tr>\r\n{2}</table>\r\n";
		string format2 = "<tr c=\"{4}\"><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>\r\n";
		string rootDir = this.compareArray[0].RootDir;
		StringBuilder stringBuilder = new StringBuilder();
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		string arg = "";
		for (int i = 0; i < this.compareArray.Count; i++)
		{
			if (this.compareArray[i].RootDir != rootDir)
			{
				arg = string.Format("文件被修改: {0}&nbsp;&nbsp;&nbsp;文件被删除: {1}&nbsp;&nbsp;&nbsp;文件未知: {2}", num, num3, num2);
				this.sb.AppendFormat(format, rootDir, arg, stringBuilder.ToString());
				rootDir = this.compareArray[i].RootDir;
				stringBuilder = new StringBuilder();
				num = 0;
				num2 = 0;
				num3 = 0;
			}
			switch (this.compareArray[i].Check)
			{
			case 0:
				num3++;
				break;
			case 2:
				num++;
				break;
			case 3:
				num2++;
				break;
			}
			stringBuilder.AppendFormat(format2, new object[]
			{
				this.compareArray[i].FilePath,
				this.compareArray[i].Length,
				this.compareArray[i].LastWriteTime.ToString(),
				this.ResultDictionary(this.compareArray[i].Check),
				this.compareArray[i].Check
			});
		}
		this.sb.AppendFormat(format, rootDir, arg, stringBuilder.ToString());
		this.checkResult.Text = this.sb.ToString();
	}
	protected string ResultDictionary(int checktype)
	{
		string result;
		switch (checktype)
		{
		case 0:
			result = "<b>被删除</b>";
			break;
		case 1:
			result = "正常";
			break;
		case 2:
			result = "<span>被修改</span>";
			break;
		case 3:
			result = "<em>未知</em>";
			break;
		default:
			result = "<em>未知</em>";
			break;
		}
		return result;
	}
	protected void CheckResult()
	{
		for (int i = 0; i < this.compareArray.Count; i++)
		{
			bool flag = false;
			for (int j = 0; j < this.standardArray.Count; j++)
			{
				if (this.standardArray[j].Check == 0)
				{
					if (this.compareArray[i].FilePath == this.standardArray[j].FilePath)
					{
						this.standardArray[j].Check = 5;
						flag = true;
						if (this.compareArray[i].MD5Code == this.standardArray[j].MD5Code)
						{
							this.compareArray[i].Check = 1;
						}
						else
						{
							this.compareArray[i].Check = 2;
						}
						break;
					}
				}
			}
			if (!flag)
			{
				this.compareArray[i].Check = 3;
			}
		}
		for (int i = 0; i < this.standardArray.Count; i++)
		{
			if (this.standardArray[i].Check == 0)
			{
				this.compareArray.Add(this.standardArray[i]);
			}
		}
		this.compareArray.Sort();
	}
	protected void CreateFile()
	{
		string path = base.Server.MapPath("") + "\\howaefile.md5";
		DirectoryInfo dr = new DirectoryInfo(base.Server.MapPath("~"));
		this.CreateMd5File(dr, "/");
		using (StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8))
		{
			streamWriter.Write(this.sb_w.ToString());
			streamWriter.Dispose();
		}
		this.WriteErrMsg("", "标准文件生成成功<br/>", "");
	}
	protected void CreateMd5File(DirectoryInfo dr, string ParentDirName)
	{
		FileInfo[] files = dr.GetFiles();
		for (int i = 0; i < files.Length; i++)
		{
			FileInfo fileInfo = files[i];
			string a = fileInfo.Extension.ToLower();
			if (a == ".aspx" || a == ".cs" || a == ".html" || a == ".htm" || a == ".css" || a == ".js")
			{
				this.sb_w.Append(string.Concat(new string[]
				{
					this.md5_hash(fileInfo.FullName),
					" *",
					ParentDirName,
					fileInfo.Name,
					"\r\n"
				}));
			}
		}
		DirectoryInfo[] directories = dr.GetDirectories();
		for (int i = 0; i < directories.Length; i++)
		{
			DirectoryInfo directoryInfo = directories[i];
			this.CreateMd5File(directoryInfo, ParentDirName + directoryInfo.Name + "/");
		}
	}
	protected void InitStandardArray()
	{
		DirectoryInfo directoryInfo = new DirectoryInfo(base.Server.MapPath(""));
		string text = base.Server.MapPath("") + "\\howaefile.md5";
		FileInfo fileInfo = new FileInfo(text);
		if (!fileInfo.Exists)
		{
			this.WriteErrMsg("", "校验原文件不存在或已经被删除!", "");
		}
		FileStream fileStream = new FileStream(text, FileMode.Open, FileAccess.Read, FileShare.Read);
		using (StreamReader streamReader = new StreamReader(text, Encoding.UTF8))
		{
			string text2;
			while ((text2 = streamReader.ReadLine()) != null)
			{
				string[] array = text2.Split(new char[]
				{
					'*'
				});
				string md = array[0].ToString().Trim();
				string filepath = array[1].ToString().Trim();
				HL_Admin_Admin_CheckFile.MD5File item = new HL_Admin_Admin_CheckFile.MD5File(md, filepath, 0);
				this.standardArray.Add(item);
			}
		}
	}
	protected void InitCompareArray()
	{
		DirectoryInfo dr = new DirectoryInfo(base.Server.MapPath("~"));
		this.CompareFile(dr, "/");
	}
	protected void CompareFile(DirectoryInfo dr, string ParentDirName)
	{
		FileInfo[] files = dr.GetFiles();
		for (int i = 0; i < files.Length; i++)
		{
			FileInfo fileInfo = files[i];
			string a = fileInfo.Extension.ToLower();
			if (a == ".aspx" || a == ".cs" || a == ".html" || a == ".htm" || a == ".css" || a == ".js")
			{
				string fileMd5Code = this.GetFileMd5Code(fileInfo.FullName);
				HL_Admin_Admin_CheckFile.MD5File mD5File = new HL_Admin_Admin_CheckFile.MD5File(fileMd5Code, ParentDirName + fileInfo.Name, 0);
				mD5File.Length = fileInfo.Length.ToString();
				mD5File.LastWriteTime = fileInfo.LastWriteTime.ToString();
				this.compareArray.Add(mD5File);
			}
		}
		DirectoryInfo[] directories = dr.GetDirectories();
		for (int i = 0; i < directories.Length; i++)
		{
			DirectoryInfo directoryInfo = directories[i];
			this.CompareFile(directoryInfo, ParentDirName + directoryInfo.Name + "/");
		}
	}
	protected string GetFileMd5Code(string filepath)
	{
		FileInfo fileInfo = new FileInfo(filepath);
		string result;
		if (fileInfo.Exists)
		{
			result = this.md5_hash(filepath);
		}
		else
		{
			result = "";
		}
		return result;
	}
	protected string CompareToStandFile(string filepath, string md5code)
	{
		filepath = base.Server.MapPath("~") + filepath;
		string fileMd5Code = this.GetFileMd5Code(filepath);
		string result;
		if (fileMd5Code == "")
		{
			result = "-1";
		}
		else
		{
			if (fileMd5Code == md5code)
			{
				result = "1";
			}
			else
			{
				result = "2";
			}
		}
		return result;
	}
	protected string md5_img(string filename)
	{
		FileInfo fi = new FileInfo(filename);
		return this.md5_img(fi);
	}
	protected string md5_img(FileInfo fi)
	{
		string result;
		if (fi.Exists)
		{
			string str = fi.LastWriteTime.ToString();
			string str2 = fi.Length.ToString();
			string text = Text.Md5(str + "-" + str2);
			result = text;
		}
		else
		{
			result = "delete";
		}
		return result;
	}
	public string md5_hash(string path)
	{
		string result;
		try
		{
			FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
			MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
			byte[] value = mD5CryptoServiceProvider.ComputeHash(fileStream);
			string text = BitConverter.ToString(value);
			text = text.Replace("-", "");
			fileStream.Dispose();
			mD5CryptoServiceProvider.Clear();
			result = text;
		}
		catch (Exception ex)
		{
			result = ex.ToString();
		}
		return result;
	}
}
